package com.icekrvams.billing.data

class Products private constructor(builder: Builder) {

    internal val availableSubProducts: List<String>
    private val deprecatedSubProducts: List<String>
    private val availableOneTimeIapProducts: List<String>
    private val deprecatedOneTimeIapProducts: List<String>
    internal val consumableIapProducts: List<String>

    init {
        this.availableSubProducts = builder.availableSubProducts
        this.deprecatedSubProducts = builder.deprecatedSubProducts
        this.availableOneTimeIapProducts = builder.availableIapProducts
        this.deprecatedOneTimeIapProducts = builder.deprecatedIapProducts
        this.consumableIapProducts = builder.consumableIapProducts
    }

    val allSubProducts get() = availableSubProducts + deprecatedSubProducts
    val allOneTimeIapProducts get() = availableOneTimeIapProducts + deprecatedOneTimeIapProducts
    val allAvailableProducts get() = availableOneTimeIapProducts + availableSubProducts + consumableIapProducts
    val allAvailableIapProducts get() = availableOneTimeIapProducts + consumableIapProducts

    class Builder {

        internal var availableSubProducts: List<String> = emptyList()
            private set
        internal var deprecatedSubProducts: List<String> = emptyList()
            private set
        internal var availableIapProducts: List<String> = emptyList()
            private set
        internal var deprecatedIapProducts: List<String> = emptyList()
            private set
        internal var consumableIapProducts: List<String> = emptyList()
            private set

        fun availableSubProducts(vararg products: String): Builder {
            checkValidProducts(products)
            this.availableSubProducts = products.asList()
            return this
        }

        fun deprecatedSubProducts(vararg products: String): Builder {
            checkValidProducts(products)
            this.deprecatedSubProducts = products.asList()
            return this
        }

        fun availableOneTimeIapProducts(vararg products: String): Builder {
            checkValidProducts(products)
            this.availableIapProducts = products.asList()
            return this
        }

        fun deprecatedOneTimeIapProducts(vararg products: String): Builder {
            checkValidProducts(products)
            this.deprecatedIapProducts = products.asList()
            return this
        }

        fun consumableIapProducts(vararg products: String): Builder {
            checkValidProducts(products)
            this.consumableIapProducts = products.asList()
            return this
        }

        fun build(): Products {
            if (availableSubProducts.size + deprecatedSubProducts.size + availableIapProducts.size +
                deprecatedIapProducts.size + consumableIapProducts.size == 0
            ) {
                throw IllegalStateException("No product provided")
            }
            return Products(this)
        }

        private fun checkValidProducts(products: Array<out String>) {
            if (products.isEmpty()) {
                throw IllegalArgumentException("The invoke of this method without any param is meaningless")
            }
        }
    }
}
